﻿#include "ssWorkThread.h"
//#include "../../time/ssTimer.h"
#include "ssThreadPool.h"


namespace StarSeeker
{

ssWorkThread::ssWorkThread(ssThreadPool& pool)
	:m_threadId(-1), m_weight(0), m_pool(pool)
{
}

ssWorkThread::~ssWorkThread()
{
	RequestStop();
	WaitForExit();
}

void ssWorkThread::ThreadMemberFunc()
{
	OnThreadRun();
	while (!IsRequestStop())
	{
		SS_MUTEX_LOCK(m_pool.m_mutex); //锁定互斥量
		while (m_pool.m_msgList.Count()==0 && !IsRequestStop())
		{
			m_pool.m_condGet.Wait(); //条件变量等待
		}
		ulint count = 1;
		ssMsg_s msg;
		if (m_weight >= 0)
		{
			count = m_pool.m_msgList.Count() >> m_weight;
		}
		for (ulint i = 0; i < count; ++i)
		{
			if (m_pool.m_msgList.PopFront(&msg))
			{
				m_tempList.PushBack(msg);
			}
		}
		SS_MUTEX_UNLOCK(m_pool.m_mutex);

		ssList<ssMsg_s>::Iterator itr = m_tempList.Begin();
		while(itr.IsVaild())
		{
			HandleMessage(*itr.Data());
			++itr;
		}
		m_tempList.Empty();
	}
}

StarSeeker::ulint ssWorkThread::GetMsgCount()
{
	return m_tempList.Size();
}

}
